﻿using SmallJob.Services.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BackEnd.Controllers
{
     [Authorize]
    public class TransactionController : Controller
    {
        private readonly IUserService _UserService;
        private readonly ITransactionService _TransactionService;
        public TransactionController(IUserService UserService, ITransactionService TransactionService)
        {
            _UserService = UserService;
            _TransactionService = TransactionService;
        }
        public ActionResult Index(string page, string filter, string filtertype, string filterstatus)
        {
            if (string.IsNullOrWhiteSpace(page))
            {
                page = "1";
            }
            ViewData["tableindex"] = new { page = page, pagesize = Convert.ToInt32(Resources.Resource.ItemPerPage), filter = filter, filtertype = filtertype, filterstatus = filterstatus };
            return View();
        }
        public ActionResult TableTransaction(int page, int pagesize, string filter, string filtertype, string filterstatus)
        {
            ViewData["filter"] = filter;
            ViewData["filtertype"] = filtertype;
            ViewData["filterstatus"] = filterstatus;
            int total = 0;
            pagesize = Convert.ToInt32(Resources.Resource.ItemPerPage);
            if (page == 0)
            {
                page = 1;
            }
            var model = _TransactionService.GetByPageFilter(page, pagesize, out total, filter, filtertype, filterstatus);
            foreach (var item in model)
            {
                item.Payer = _UserService.GetById(item.PayerID);
                item.Receiver = _UserService.GetById(item.ReceiverID);
            }
            if ((total % pagesize) > 0)
            {
                ViewData["total"] = (total / pagesize) + 1;
            }
            else
            {
                ViewData["total"] = total / pagesize;
            }

            ViewData["curpage"] = page;
            ViewData["nextpage"] = page + 1;
            ViewData["prepage"] = page - 1;
            return PartialView(model);
        }

    }
}
